package service

import (
	"go-demo/common"
	"go-demo/dao"
	"go-demo/model"
)

func CreateMessageSendEvent(token string, toUserId int64, actionType string, content string) interface{} {
	//验证签名是否过期
	parseToken, err := common.ParseToken(token)
	if parseToken == nil && err != nil {
		return nil
	}
	if actionType != "1" {
		return nil
	}
	userId := common.UsersLoginInfo[token].Id
	var messageSendEvent model.MessageSendEvent
	messageSendEvent.UserId = userId
	messageSendEvent.ToUserId = toUserId
	messageSendEvent.MsgContent = content
	dao.CreateMessageSendEvent(messageSendEvent)
	return common.SuccessCode
}

func CreateMessageChat(token string, toUserId int64) interface{} {
	//验证签名是否过期
	parseToken, err := common.ParseToken(token)
	if parseToken == nil && err != nil {
		return nil
	}
	userId := common.UsersLoginInfo[token].Id
	var messageSendEvent model.MessageSendEvent
	var messageList []model.MessageSendEvent
	messageSendEvent.UserId = userId
	messageSendEvent.ToUserId = toUserId
	messageA, messageB := dao.GetMessageSendEventByUsers(messageSendEvent)
	messageA = append(messageA, messageB...)
	for _, message := range messageA {
		if message.IsViewed == false {
			//更新消息的状态
			dao.UpdateMessageStatus(message)
			messageList = append(messageList, message)
		}
	}
	return messageList
}
